#include "Timestamp.h"
#include <ctime>
#include <iomanip>
#include <sstream>

Timestamp::Timestamp()
    : secsinceepoch_(std::time(nullptr)) // 当前时间（单位：秒）
{
}

Timestamp::Timestamp(int64_t secsinceepoch)
    : secsinceepoch_(static_cast<time_t>(secsinceepoch)) // 显式转换，确保类型一致
{
}

Timestamp Timestamp::now()
{
    return Timestamp(std::time(nullptr));
}

time_t Timestamp::toint() const
{
    return secsinceepoch_;
}

std::string Timestamp::tostring() const
{
    char buf[32] = {};
    tm *tm_time = localtime(&secsinceepoch_);
    snprintf(buf, 32, "%4d-%02d-%02d %02d:%02d:%02d",
             tm_time->tm_year + 1900,
             tm_time->tm_mon + 1,
             tm_time->tm_mday,
             tm_time->tm_hour,
             tm_time->tm_min,
             tm_time->tm_sec);
    return buf;
}

// #include <unistd.h>
// #include <iostream>

// int main()
// {
//     Timestamp ts;
//     std::cout << ts.toint() << std::endl;
//     std::cout << ts.tostring() << std::endl;

//     sleep(1);
//     std::cout << Timestamp::now().toint() << std::endl;
//     std::cout << Timestamp::now().tostring() << std::endl;
// }